package api

import (
	"context"
	"getaway/basic/config"
	__ "getaway/basic/proto/orderproto"
	"getaway/handler/request"
	"getaway/handler/response"
	"getaway/middleware"
	"github.com/gin-gonic/gin"
)

// CancelOrder
// @Summary      取消订单
// @Description  取消订单
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.CancelOrder    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/cancelOrder [post]
// @Security TokenAuth
func CancelOrder(c *gin.Context) {
	userId := c.GetUint("userId")
	authorityId := c.GetUint("authorityId")
	var req request.CancelOrder
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	// 取消订单
	res, err := config.OrderClient.CanCelOrder(c, &__.CanCelOrderReq{
		OrderId:         int64(req.OrderId),
		Reason:          req.Reason,
		Liability:       req.Liability,
		CancellationFee: req.CancellationFee,
		UserId:          int64(userId),
		Identity:        int64(authorityId),
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}

	// 记录取消订单行为
	middleware.RecordClickBehavior(c, userId, middleware.ClickCancelOrder, map[string]interface{}{
		"orderId":   req.OrderId,
		"reason":    req.Reason,
		"liability": req.Liability,
		"success":   err == nil,
	})

	response.Success(c, "取消订单成功", res)
}

// GetCancelOrderList
// @Summary      取消订单列表
// @Description  获取用户已取消的订单列表
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.IsCanCelOrderReq    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/cancelOrderList [get]
// @Security TokenAuth
func GetCancelOrderList(c *gin.Context) {
	userId := c.GetUint("userId")
	authorityId := c.GetUint("authorityId")
	var req request.IsCanCelOrderReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	if authorityId != 3 {

		if req.UserId == 0 {
			response.ParameterError(c, "参数错误", "用户id不能为空")
			return
		}
		list, err := config.OrderClient.IsCanCelOrder(c, &__.IsCanCelOrderReq{
			UserId: int64(req.UserId),
		})
		// 记录查询已取消订单行为
		middleware.RecordClickBehavior(c, userId, "查询已取消订单", map[string]interface{}{
			"userId":  req.UserId,
			"success": err == nil,
		})
		if err != nil {
			response.SrvError(c, "服务端错误", err.Error())
			return
		}
		response.Success(c, "查询已取消订单成功", list)
	}

	list, err := config.OrderClient.IsCanCelOrder(c, &__.IsCanCelOrderReq{
		UserId: int64(req.UserId),
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	// 记录查询已取消订单行为
	middleware.RecordClickBehavior(c, userId, "查询已取消订单", map[string]interface{}{
		"userId":  userId,
		"success": err == nil,
	})
	response.Success(c, "查询已取消订单成功", list)
}

// GetTrajectoryList
// @Summary      订单行动轨迹
// @Description  订单行动轨迹
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   query    request.GetId    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/trajectoryList [get]
// @Security TokenAuth
func GetTrajectoryList(c *gin.Context) {
	var req request.GetId
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	list, err := config.OrderClient.TrajectoryList(c, &__.TrajectoryListReq{
		OrderId: req.Id,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	// 记录查询订单行动轨迹行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "查询订单行动轨迹", map[string]interface{}{
		"orderId": req.Id,
		"success": err == nil,
	})
	response.Success(c, "查询订单行动轨迹成功", list)
}

// RecordLocation
// @Summary      记录轨迹经纬度
// @Description  记录轨迹经纬度
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.GetId    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/recordLocation [post]
// @Security TokenAuth
func RecordLocation(c *gin.Context) {
	var req request.GetId
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	_, err := config.OrderClient.RecordLocation(c, &__.RecordLocationReq{
		OrderId: req.Id,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "记录轨迹经纬度", map[string]interface{}{
		"orderId": req.Id,
		"success": err == nil,
	})
	response.Success(c, "记录成功", nil)
}

// ReceiveLocationInfo
// @Summary      接收位置经纬度
// @Description  接收位置经纬度
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.ReceiveLocationReq    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/receiveLocationInfo [post]
// @Security TokenAuth
func ReceiveLocationInfo(c *gin.Context) {
	var req request.ReceiveLocationReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	_, err := config.OrderClient.ReceiveLocation(c, &__.ReceiveLocationReq{
		OrderId:   req.OrderId,
		Latitude:  req.Latitude,
		Longitude: req.Longitude,
		Point:     req.Point,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	// 记录接收位置经纬度行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "接收位置经纬度", map[string]interface{}{
		"orderId":   req.OrderId,
		"latitude":  req.Latitude,
		"longitude": req.Longitude,
		"success":   err == nil,
	})
	response.Success(c, "接收成功", nil)
}

// UpdateOrderStatus
// @Summary      修改订单状态
// @Description  修改订单状态
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.UpdateOrderStatusReq    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/updateOrderStatus [post]
// @Security TokenAuth
func UpdateOrderStatus(c *gin.Context) {
	var req request.UpdateOrderStatusReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	Orderinfo, err := config.OrderClient.UpdateOrderStatus(c, &__.UpdateOrderStatusReq{
		OrderId: req.OrderId,
		Status:  req.Status,
		Mobile:  req.Mobile,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	SetUpOrOutCarLoc(c, req)
	// 记录修改订单状态行为
	userId := c.GetUint("userId")
	middleware.RecordClickBehavior(c, userId, "修改订单状态", map[string]interface{}{
		"orderId": req.OrderId,
		"status":  req.Status,
		"success": err == nil,
	})
	response.Success(c, "修改订单状态成功", Orderinfo)
}

// 根据状态自动标记位置特征
func SetUpOrOutCarLoc(c *gin.Context, req request.UpdateOrderStatusReq) {
	// 接到乘客标记为上车点
	if req.Status == 3 {
		_, err := config.OrderClient.TrajectorySet(c, &__.TrajectorySetReq{
			OrderId:   req.OrderId,
			Latitude:  req.Latitude,
			Longitude: req.Longitude,
			Point:     2,
		})
		if err != nil {
			response.SrvError(c, "服务端错误", err.Error())
			return
		}
	}
	// 乘客下车标记为下车点
	if req.Status == 4 {
		_, err := config.OrderClient.TrajectorySet(c, &__.TrajectorySetReq{
			OrderId:   req.OrderId,
			Latitude:  req.Latitude,
			Longitude: req.Longitude,
			Point:     3,
		})
		if err != nil {
			response.SrvError(c, "服务端错误", err.Error())
			return
		}
	}
}

// OrderCancellationDel
// @Summary      订单取消删除
// @Description  订单取消删除
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.OrderCancellationDel    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/orderCancellationDel [post]
func OrderCancellationDel(c *gin.Context) {
	var req request.OrderCancellationDel
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	del, err := config.OrderClient.OrderCancellationDel(context.Background(), &__.OrderCancellationDelReq{
		Id:     req.Id,
		UserId: req.UserId,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	// 记录订单取消删除行为
	userId := req.UserId
	middleware.RecordClickBehavior(c, userId, "订单取消删除", map[string]interface{}{
		"id":      req.Id,
		"userId":  req.UserId,
		"success": err == nil,
	})
	response.Success(c, "删除成功", del)
}

// OrderCancellationDetail
// @Summary      订单取消详情
// @Description  订单取消详情
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   query    request.OrderCancellationDetail    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/orderCancellationDetail [get]
func OrderCancellationDetail(c *gin.Context) {
	var req request.OrderCancellationDetail
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	detail, err := config.OrderClient.OrderCancellationDetail(context.Background(), &__.OrderCancellationDetailReq{
		Id:     req.Id,
		UserId: req.UserId,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	// 记录订单取消详情行为
	userId := req.UserId
	middleware.RecordClickBehavior(c, userId, "订单取消详情", map[string]interface{}{
		"id":      req.Id,
		"userId":  req.UserId,
		"success": err == nil,
	})
	response.Success(c, "查询详情成功", detail)
}

// ReassignCreate
// @Summary      订单改派创建
// @Description  订单改派创建
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.ReassignCreate    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Security TokenAuth
// @Router       /order/reassignCreate [post]
func ReassignCreate(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.ReassignCreate
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	create, err := config.OrderClient.ReassignCreate(context.Background(), &__.ReassignCreateReq{
		OrderId:      req.OrderId,
		UserId:       uint64(userId),   //操作人
		AssignDriver: req.AssignDriver, //新司机
		Reason:       req.Reason,       //新司机原因
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	// 记录订单改派创建行为
	middleware.RecordClickBehavior(c, userId, "订单改派创建", map[string]interface{}{
		"orderId":      req.OrderId,
		"assignDriver": req.AssignDriver,
		"reason":       req.Reason,
		"success":      err == nil,
	})
	response.Success(c, "改派创建成功", create.Id)
}

// CreateOrder
// @Summary      创建订单
// @Description  创建订单
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.CreateOrderReq    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/createOrder [post]
// @Security TokenAuth
func CreateOrder(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.CreateOrderReq
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	if req.OrderType == 2 || req.OrderType == 3 || req.OrderType == 4 {
		response.ParameterError(c, "预约时间不能为空", nil)
		return
	}
	_, err := config.OrderClient.OrderCreate(c, &__.OrderCreateReq{
		UserId:           uint64(userId),
		CarTypeId:        uint64(req.CarTypeId),
		StartLoc:         req.StartLoc,
		EndLoc:           req.EndLoc,
		OrderType:        req.OrderType,
		ServiceStartTime: req.ServiceStartTime,
		CityId:           req.CityId,
		Source:           req.Source,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	// 记录创建订单行为
	middleware.RecordClickBehavior(c, userId, middleware.ClickSubmitOrder, map[string]interface{}{
		"carTypeId": req.CarTypeId,
		"startLoc":  req.StartLoc,
		"endLoc":    req.EndLoc,
		"orderType": req.OrderType,
		"success":   err == nil,
	})
	response.Success(c, "创建成功", nil)
}

// 订单取消列表状态
func CancelOrderStatusList(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.CancelOrderStatusList
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	list, err := config.OrderClient.OrderCancellationList(context.Background(), &__.OrderCancellationListReq{
		UserId:   uint64(userId),
		Page:     req.Page,
		PageSize: req.PageSize,
		Status:   req.Status,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	// 记录查询订单取消列表状态行为
	middleware.RecordClickBehavior(c, userId, "查询订单取消列表状态", map[string]interface{}{
		"page":     req.Page,
		"pageSize": req.PageSize,
		"status":   req.Status,
		"success":  err == nil,
	})
	response.Success(c, "查询成功", list)
}

// OrderComment
// @Summary      订单评论
// @Description  订单评论
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.OrderComment    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/orderComment [post]
// @Security TokenAuth
func OrderComment(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.OrderComment
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	_, err := config.OrderClient.OrderComment(context.Background(), &__.OrderCommentReq{
		UserId:  uint32(userId),
		OrderId: uint32(req.OrderId),
		Comment: req.Comment,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	// 记录订单评论行为
	middleware.RecordClickBehavior(c, userId, "订单评论", map[string]interface{}{
		"orderId": req.OrderId,
		"comment": req.Comment,
		"success": err == nil,
	})
	response.Success(c, "评论成功", nil)
}

// 订单详情
func OrderDetail(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.OrderDetail
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	detail, err := config.OrderClient.OrderDetail(context.Background(), &__.OrderDetailReq{
		Id:     req.Id,
		UserId: uint64(userId),
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "详情查询成功", detail)
}

// 订单时间详情
func OrderTimeDetail(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.OrderTimeDetail
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	detail, err := config.OrderClient.OrderTimeDetail(context.Background(), &__.OrderTimeDetailReq{
		Id:     req.Id,
		UserId: uint64(userId),
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "查询时间详情成功", detail)
}

// PricingInformationAdd
// @Summary      订单价格信息添加
// @Description  订单价格信息添加
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.PricingInformationAdd    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/pricingInformationAdd [post]
// @Security TokenAuth
func PricingInformationAdd(c *gin.Context) {
	var req request.PricingInformationAdd
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	add, err := config.OrderClient.PricingInformationAdd(context.Background(), &__.PricingInformationAddReq{
		OrderId:         req.OrderId,
		PricesStarting:  req.PricesStarting,
		MileageFee:      req.MileageFee,
		DurationFee:     req.DurationFee,
		LongDistance:    req.LongDistance,
		Bridge:          req.Bridge,
		NightServiceFee: req.NightServiceFee,
		ParkingFee:      req.ParkingFee,
		Other:           req.Other,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "价格信息添加成功", add.Id)
}

// 订单价格信息详情
func PricingInformationDetail(c *gin.Context) {
	var req request.PricingInformationDetail
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	detail, err := config.OrderClient.PricingInformationDetail(context.Background(), &__.PricingInformationDetailReq{Id: req.Id})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "查询价格详情成功", detail)
}

func OrderPaymentCallback(c *gin.Context) {
	tradeStatus := c.PostForm("trade_status")
	outTradeNo := c.PostForm("out_trade_no")
	if tradeStatus == "TRADE_SUCCESS" {
		config.OrderClient.OrderPaymentCallback(c, &__.OrderPaymentCallbackReq{
			OrderSn: outTradeNo,
			Status:  5,
		})
	} else if tradeStatus == "TRADE_CLOSED" {
		config.OrderClient.OrderPaymentCallback(c, &__.OrderPaymentCallbackReq{
			OrderSn: outTradeNo,
			Status:  6,
		})
	} else if tradeStatus == "TRADE_FINISHED" {
		config.OrderClient.OrderPaymentCallback(c, &__.OrderPaymentCallbackReq{
			OrderSn: outTradeNo,
			Status:  7,
		})

	} else if tradeStatus == "WAIT_BUYER_PAY" {
		return
	}
	c.Writer.WriteString("success")
	return
}

// OrderDriverId
// @Summary      司机接单
// @Description  司机接单
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.OrderDriverId    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/orderDriverId [post]
// @Security TokenAuth
func OrderDriverId(c *gin.Context) {
	userId := c.GetUint("userId")
	authorityId := c.GetUint("authorityId")
	if !(authorityId == 1 || authorityId == 2) {
		response.ParameterError(c, "参数错误", "非管理员不能操作")
		return
	}
	var req request.OrderDriverId
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	driver, err := config.OrderClient.OrderDriverId(context.Background(), &__.OrderDriverIdReq{
		OrderId:  req.OrderId,
		DriverId: req.DriverId,
		UserId:   uint64(userId),
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "司机接单成功", driver)
}

// DoubtUpdateStatus
// @Summary      修改订单疑义状态
// @Description  修改订单疑义状态
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.DoubtUpdateStatus    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/doubtUpdateStatus [post]
// @Security TokenAuth
func DoubtUpdateStatus(c *gin.Context) {
	userId := c.GetUint("userId")
	if !(userId == 1 || userId == 2) {
		response.ParameterError(c, "参数错误", "非管理员不能操作")
		return
	}
	var req request.DoubtUpdateStatus
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	doubt, err := config.OrderClient.DoubtUpdateStatus(context.Background(), &__.DoubtUpdateStatusReq{
		Id:     req.Id,
		Status: req.Status,
		UserId: uint64(userId),
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "订单疑义修改状态成功", doubt)
}

// CouponTemplateAdd
// @Summary      优惠券模板创建
// @Description  优惠券模板创建
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.CouponTemplateAdd    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/couponTemplateAdd [post]
// @Security TokenAuth
func CouponTemplateAdd(c *gin.Context) {
	userId := c.GetUint("userId")
	if !(userId == 1 || userId == 2) {
		response.ParameterError(c, "参数错误", "非管理员不能操作")
		return
	}
	var req request.CouponTemplateAdd
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	add, err := config.OrderClient.CouponTemplateAdd(context.Background(), &__.CouponTemplateAddReq{
		Title:       req.Title,          // 优惠券名称
		CouponType:  req.CouponType,     // 优惠券类型
		Description: req.Description,    // 优惠券使用规则
		Price:       float32(req.Price), // 优惠券面额
		Discount:    req.Discount,       // 折扣 1-8折 2-8.5折 3-9折 4-9.5折
		Limit:       req.Limit,          // 优惠券限定 0-无限制 1-经济型 2-豪华型
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "优惠券模板创建成功", add)
}

// UpdateReassignStatus
// @Summary      修改改派状态
// @Description  修改改派状态
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.UpdateReassignStatus    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/updateReassignStatus [post]
// @Security TokenAuth
func UpdateReassignStatus(c *gin.Context) {
	var req request.UpdateReassignStatus
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	status, err := config.OrderClient.UpdateReassignStatus(context.Background(), &__.UpdateReassignStatusReq{
		Id:     req.Id,
		Status: req.Status,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "改派状态修改成功", status)
}

// GetUserIdOrderInfo
// @Summary      用户查看订单列表
// @Description  用户查看订单列表
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   query    request.GetUserIdOrderInfo    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/getUserIdOrderInfo [get]
// @Security TokenAuth
func GetUserIdOrderInfo(c *gin.Context) {
	userId := c.GetUint("userId")
	var req request.GetUserIdOrderInfo
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	info, err := config.OrderClient.GetUserIdOrderInfo(context.Background(), &__.GetUserIdOrderInfoReq{
		UserId: uint32(userId),
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "用户查看订单列表成功成功", info)
}

// UserCouponAdd
// @Summary      用户添加优惠券
// @Description  用户添加优惠券
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.UserCouponAdd    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/userCouponAdd [post]
// @Security TokenAuth
func UserCouponAdd(c *gin.Context) {
	var req request.UserCouponAdd
	authorityId := c.GetUint("authorityId")
	// 验证请求参数
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	if authorityId != 3 {
		response.ParameterError(c, "参数错误", "非管理员不能执行优惠券添加操作")
		return
	}

	add, err := config.OrderClient.UserCouponAdd(context.Background(), &__.UserCouponAddReq{
		UserId:   req.UserId,
		CouponId: req.CouponId,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "用户优惠券添加成功", add)
}

// UserCollectAdd
// @Summary      用户收藏优惠券
// @Description  用户收藏优惠券
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.UserCollectAdd    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/userCollectAdd [post]
// @Security TokenAuth
func UserCollectAdd(c *gin.Context) {
	userId := c.GetUint("userId")
	authorityId := c.GetUint("authorityId")
	var req request.UserCollectAdd
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	if !(authorityId == 2 || authorityId == 3) {
		response.ParameterError(c, "参数错误", "非乘客不能执行收藏操作")
		return
	}

	add, err := config.OrderClient.UserCollectAdd(context.Background(), &__.UserCollectAddReq{
		UserId:   uint64(userId),
		CouponId: req.CouponId,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "用户收藏优惠券成功", add)
}

// UserShareCouponsAdd
// @Summary      用户分享优惠券添加
// @Description  用户分享优惠券添加
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.UserShareCouponsAdd    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/userShareCouponsAdd [post]
// @Security TokenAuth
func UserShareCouponsAdd(c *gin.Context) {
	var req request.UserShareCouponsAdd
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	add, err := config.OrderClient.UserShareCouponsAdd(context.Background(), &__.UserShareCouponsAddReq{
		SharerId:   req.SharerId,
		CouponId:   req.CouponId,
		ShareToke:  req.ShareToke,
		ReceiverId: req.ReceiverId,
		ExpireTime: req.ExpireTime,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "用户分享优惠券添加成功", add)
}

// CouponTemplateList
// @Summary      优惠券模板列表
// @Description  优惠券模板列表
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   query    request.CouponTemplateList    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/couponTemplateList [get]
// @Security TokenAuth
func CouponTemplateList(c *gin.Context) {
	var req request.CouponTemplateList
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	list, err := config.OrderClient.CouponTemplateList(context.Background(), &__.CouponTemplateListReq{})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "优惠券模板列表成功", list)
}

// CouponTemplateSearch
// @Summary      优惠券模板搜索
// @Description  优惠券模板搜索
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   query    request.CouponTemplateSearch    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/couponTemplateSearch [get]
// @Security TokenAuth
func CouponTemplateSearch(c *gin.Context) {
	var req request.CouponTemplateSearch
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	search, err := config.OrderClient.CouponTemplateSearch(context.Background(), &__.CouponTemplateSearchReq{Title: req.Title})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "优惠券模板列表成功", search)

}

// CouponSharesAdd
// @Summary      优惠券分享添加
// @Description  优惠券分享添加
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.CouponSharesAdd    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/couponSharesAdd [post]
// @Security TokenAuth
func CouponSharesAdd(c *gin.Context) {
	var req request.CouponSharesAdd
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	add, err := config.OrderClient.CouponSharesAdd(context.Background(), &__.CouponSharesAddReq{
		CouponId: req.CouponId,
		ShareBy:  req.ShareBy,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "优惠券分享添加成功", add)
}

// OrderCancellationUpdateStatus
// @Summary      修改取消订单状态
// @Description  修改取消订单状态
// @Tags         订单模块
// @Accept       json
// @Produce      json
// @Param        order   body    request.OrderCancellationUpdateStatus    true  "用户参数"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.Response
// @Failure      500  {object}  response.Response
// @Router       /order/OrderCancellationUpdateStatus [post]
// @Security TokenAuth
func OrderCancellationUpdateStatus(c *gin.Context) {
	userId := c.GetUint("userId")
	if !(userId == 1 || userId == 2) {
		response.ParameterError(c, "参数错误", "非管理员不能操作")
		return
	}
	var req request.OrderCancellationUpdateStatus
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}

	status, err := config.OrderClient.OrderCancellationUpdateStatus(context.Background(), &__.OrderCancellationUpdateStatusReq{
		Id:     req.Id,
		Status: req.Status,
	})
	if err != nil {
		response.SrvError(c, "服务端错误", err.Error())
		return
	}
	response.Success(c, "修改取消订单状态成功", status)
}
